//
//  SliderBar.m
//  Anyair
//
//  Created by karlcool on 14-5-16.
//  Copyright (c) 2014年 Karlcool. All rights reserved.
//

#import "SliderBar.h"
#import "BluetoothViewController.h"
#import "ControlViewController.h"
#import "ShareBoard.h"
#import "LogInViewController.h"

@implementation SliderBar

+ (SliderBar*)createSliderBar {
    
    if (myBar==nil) {
        myBar = [[SliderBar alloc]init];
    }
    
    return myBar;
}

- (void)showSideBarInViewController:(UIViewController *)controller {
    NSArray *images = @[[UIImage imageNamed:@"Menu04"],[UIImage imageNamed:@"Menu02"],[UIImage imageNamed:@"Menu01"],[UIImage imageNamed:@"Menu03"],];
    
    NSArray *titles;
    
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isLogIn"]) {
        titles = @[@"退出账号",@"蓝牙状态",@"推荐我们",@"关于我们",];
    } else {
        titles = @[@"登陆账号",@"蓝牙状态",@"推荐我们",@"关于我们",];
    }
    
    sideBar = [[RNFrostedSidebar alloc]initWithImages:images AndTitle:titles];
    sideBar.delegate = self;
    sideBar.showFromRight = YES;
    sideBar.width = 140;
    currentViewController = controller;
    [sideBar showInViewController:controller animated:YES];
}

- (void)sidebar:(RNFrostedSidebar *)sidebar didTapItemAtIndex:(NSUInteger)index {
    
    [sidebar dismiss];
    if (index==0) {
        
        if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isLogIn"]) {
            MBProgressHUD *shortHub = [[MBProgressHUD alloc]initWithView:[UIApplication sharedApplication].keyWindow];
            [[UIApplication sharedApplication].keyWindow addSubview:shortHub];
            [shortHub setLabelText:@"已退出账号"];
            [shortHub setMode:MBProgressHUDModeText];
            [shortHub hide:YES afterDelay:1];
            [shortHub show:YES];
            [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"isLogIn"];
        } else {
            LogInViewController *logIn = [[LogInViewController alloc]init];
            logIn.tempLogIn = YES;
            UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:logIn];
            navi.navigationBarHidden = YES;
            [currentViewController presentViewController:navi animated:YES completion:^{
                ;
            }];
        }
        
        
        
        
    } else if (index==1) {
        
        BluetoothViewController *bv = [[BluetoothViewController alloc]init];
        bv.view.alpha = 0;
        [currentViewController addChildViewController:bv];
        [currentViewController.view addSubview:bv.view];
        [UIView animateWithDuration:0.2 animations:^{
            bv.view.alpha = 1;
        }];
        
    } else if (index==2) {
        ShareBoard *shareView = [[ShareBoard alloc]initWithFrame:[UIScreen mainScreen].bounds];
        [[UIApplication sharedApplication].keyWindow addSubview:shareView];
        
    } else if (index==3) {
        UIAlertView *aboutAlert = [[UIAlertView alloc]initWithTitle:@"关于" message:@"Anyair1.0.0" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [aboutAlert show];
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
//    if (buttonIndex==1) {
//        ControlViewController *control = [[ControlViewController alloc]init];
//        [currentViewController.navigationController pushViewController:control animated:YES];
//    }
}

@end
